﻿using System;
using System.Collections.Generic;
using System.Linq;
using BitButterECS.Event;
using BitButterECS.Utils;

namespace BitButterECS.System
{
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
	public class UpdateOnEventAttribute : Attribute
	{
		public UpdateOnEventAttribute(params Type[] events)
		{
			Events = events;
		}

		public Type[] Events { get; }

		public static IEnumerable<Type> GetListeningEvents(object system)
		{
			var result = new List<Type>();
			var attrs = system.GetType().GetCustomAttributes(typeof(UpdateOnEventAttribute), true);
			if (attrs.Length > 0)
			{
				var attr = attrs[0] as UpdateOnEventAttribute;
				if (attr != null)
				{
					result.AddRange(attr.Events.Where(x => x != typeof(BaseEvent<>) && ECSUtils.IsSubclassOfRawGeneric(typeof(BaseEvent<>), x)));
				}
			}
			return result;
		}
	}
}
